{extend name="public:layout" /}
{block name="title"}{$moudleName}列表{/block}
{block name="head"}{/block}
{block name="body"}
<div class="wrapper wrapper-content">
    <div class="ibox">
        <div class="ibox-title">
            <ol class="breadcrumb">
                <ol class="breadcrumb">
                    <li class="active"><i class="fa fa-hand-o-right"></i> {$moudleName}列表</li>
                </ol>
            </ol>
        </div>
        <div class="ibox-content">
            <div class="toolbar">
                <form class="form-inline" id="searchForm" method="post">
                    <div class="form-group">
                        <div class="input-group">
                            <select name="service_status" class="form-control" onchange="refresh();">
                                <option value="-1">全部</option>
                                <option value="0">维修申请</option>
                                <option value="1">维修中</option>
                                <option value="2">维修完成</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="input-group">
                            <input class="form-control" type="text" placeholder="请输入车牌号" name="car_sn" />
                        </div>
                    </div>
                    <button type="button" class="btn btn-info" onclick="refresh()"><i class="fa fa-search"></i> 搜索</button>
                    <a class="btn btn-white" href="{:url('index',['mid'=>$Request.param.mid])}">
                        <i class="fa fa-trash"></i> 清空
                    </a>
                </form>
            </div>
            <div id="buttons">
                <button type="button" onclick="upOrder();" class="btn btn-info">
                    <i class="fa fa-sort"></i> 排序
                </button>
                <button type="button" onclick="batchDel(CheckboxValue())" class="btn btn-danger">
                    <i class="fa fa-trash"></i> 删除
                </button>
            </div>
            <table class="table table-bordered" id="grid"></table>
        </div>
    </div>
</div>
{/block}
{block name="script"}
<script type="text/javascript">
    $(function () {
        //列定义
        var columnsData=[
            {field: 'state', checkbox:true,},
            {field: 'aid', width:80, sortable:true, title: '编号'},
            {field: 'service_status', width:100, sortable:true, title: '维修状态',formatter:function (value,row,index) {
                    var arr=['<span class="label label-warning">提交申请</span>','<span class="label label-success">开始处理</span>','<span class="label label-primary">处理完成</span>'];
                    return arr[value];
                }},
            {field: 'car_sn', title: '车牌号'},
            {field: 'ser_type', title: '种类',sortable:true,
                formatter:function (value,row,index) {
                    var arr=['维修','保养'];
                    return arr[value];
                }},
            {field: 'title', title: '车辆问题'},
            {field: 'ser_start_time', title: '开始时间',formatter:function (value,row,index) {
                    if(value=='1970-01-01 08:00:00'){
                        return '等待维修开始';
                    }else{
                        return value;
                    }
                }},
            {field: 'ser_end_time', title: '结束时间',formatter:function (value,row,index) {
                    if(value=='1970-01-01 08:00:00'){
                        return '等待维修结束';
                    }else{
                        return value;
                    }
                }},
            {field: 'service_money', title: '维修费用'},
            {field: 'operate', title: '操作', width:120, align: 'center',
                formatter:function (value,row,index) {
                    if(row.service_status==0){
                        var btn='<a href="javascript:;" onclick="repairStar(this)" data-aid="'+row.aid+'" data-carid="'+row.car_id+'" data-type="'+row.ser_type+'" class="btn btn-warning btn-xs">开始处理</a>&nbsp;';
                        btn+='<a href="javascript:;" class="btn btn-danger btn-xs" onclick="doAction(this,refresh);"\n' +
                            '                           data-msg="确认删除吗？" data-url="__URL__/del.html" data-params="aid='+row.aid+'&mid='+row.mid+'">删除</a>';
                    }
                    if(row.service_status==1){
                        var btn='<a href="javascript:;" onclick="layerAction(this)" data-url="__URL__/edit.html?tpl=car_repair_over&aid='+row.aid+'&mid='+row.mid+'" class="btn btn-primary btn-xs">处理完成</a>&nbsp;';
                    }
                    if(row.service_status==2){
                        var btn='<a href="javascript:;" onclick="layerAction(this)" data-url="__URL__/edit.html?tpl=car_repair_info&aid='+row.aid+'&mid='+row.mid+'" class="btn btn-success btn-xs">维修详情</a>&nbsp;';
                    }
                    return btn;
                }
            }
        ];
        initTable('__URL__/pagelist.html?mid={$Request.param.mid}',columnsData,function (params) {
            params.op="{$Request.param.op}";
            params.car_sn=$("input[name='car_sn']").val();
            params.service_status=$("select[name='service_status']").val();
            return params;
        })
    });
    //刷新表格
    function refresh() {
        $('#grid').bootstrapTable('refresh');
    }
    //添加
    function add() {
        var mid="{$Request.param.mid}";
        location.href="__URL__/add.html?mid="+mid;
    }
    //批量删除文章
    function batchDel(aid) {
        if (IsMore(aid)) {
            var url = "{:url('del')}";
            var params = {'aid': aid, 'mid': "{$Request.param.mid}"};
            doConfirm('你确认要删除吗？', url, params, function (data) {
                if (data.code == 1) {
                    rePage();
                }
            });
        }
    }
    //更新排序
    function upOrder() {
        if ($(".batchSort").length == 0) {
            alert('您还没有添加内容！');
            return false;
        }
        var params = $(".batchSort").serialize();
        var url = "__URL__/uporder.html?mid={$Request.param.mid}";
        getAjax(url, params, function (data) {
            if (data.code == 1) {
                rePage();
            }
        });
    }
    /*维修开始设置车辆状态为维修中*/
    function repairStar(obj) {
        var time=Date.parse(new Date())/1000;
        //维修更新数据
        var data={aid:$(obj).data('aid'),mid:15,is_update:1,service_status:1,ser_start_time:time};
        //汽车数据更新
        var car_data={aid:$(obj).data('carid'),mid:5,is_update:1,car_status:parseInt($(obj).data('type'))+2};
        var url="{:url('logic.query/infoRecord')}";

        In('layer',function () {
            $.post('/system/logic.query/getcarstatus.html',{car_id:$(obj).data('carid')},function (res) {
                console.log(res);
                if(res==1){
                    layer.msg('车辆还在出车中，请先收车后才能开始维修！',{time:1500});
                }else{
                    layer.confirm('确定开始处理吗？确定后将更新车辆信息为维修状态，直到完成后才可以正常出车。', {
                        btn: ['确定','取消'] //按钮
                    }, function(){
                        $.post(url,data,function (res) {
                            if(res.code==1){
                                $.post(url,car_data,function (res) {
                                    if(res.code==1){
                                        layer.closeAll();
                                        refresh();
                                    }else{
                                        layer.msg(res.msg,{time:800})
                                    }
                                })
                            }else{
                                layer.msg(res.msg,{time:800})
                            }
                        })
                    });
                }
            })
        })
    }
    /*维修完成，填写维修相关信息*/
    function layerAction(obj){
        In('layer',function () {
            top.layer.open({
                type: 2,
                title:$(obj).data('title'),
                shadeClose: true,
                shade:[0.3, '#000'],
                maxmin: true, //开启最大化最小化按钮
                area: ['800px', '90%'],
                content:$(obj).data('url'),
                end:function () {
                    refresh();
                }
            });
        })
    }
</script>
{/block}